package com.sb.mvc;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.sb.model.Student;

@Controller
@Scope("request")
@SessionAttributes("currentTab")
@RequestMapping("/home")
public class HomeController {
	
	public String currentTab = "Home";
	List<Student> students = new ArrayList<Student>();
	
	@RequestMapping(method=RequestMethod.GET)
	public String home(Model model) {
		
		Student studentOne = createStudent(1,1,"Kartik Kaushik");
		Student studentTwo = createStudent(2,1,"Shankar Bhardwaj");
		students.add(studentOne);
		students.add(studentTwo);
		
		model.addAttribute("allStudents", students);
		model.addAttribute("myKey", studentOne);
		return "home";
	}
	
	@RequestMapping(value="view",method=RequestMethod.GET)
	public String view(Model model) {
		//List<Student> students = new ArrayList<Student>();
		Student studentOne = createStudent(1,1,"Kartik Kaushik");
		Student studentTwo = createStudent(2,1,"Shankar Bhardwaj");
		students.add(studentOne);
		students.add(studentTwo);
		
		model.addAttribute("allStudents", students);
		model.addAttribute("myKey", studentOne);
		return "home";
	}

	private Student createStudent(int id, int version, String name) {
		Student student = new Student();
		student.setId(id);
		student.setVersion(version);
		student.setName(name);
		return student;
	}
	
}
